package cn.xerllent.wxtp.handler;

import java.util.Map;

import org.springframework.stereotype.Component;

import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

/**
 * @author sxh
 * 推送suite_ticket
 */
@Component
@Slf4j
public class SuiteTicketHandler extends AbstractHandler {


	  @Override
	  public WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage,
	              Map<String, Object> context,
	              WxCpTpService wxCpTpService,
	              WxSessionManager sessionManager) {
			String suitId = wxMessage.getSuiteId();
			String suiteTicket = wxMessage.getSuiteTicket();
			Integer timeStamp = Convert.toInt(wxMessage.getTimeStamp());
			log.info("推送ticket成功:\n{}", suiteTicket);
			WxCpTpConfigStorage tpConfig = wxCpTpService.getWxCpTpConfigStorage();
			tpConfig.updateSuiteTicket(suiteTicket, 20 * 60);
			log.info("suit ticket缓存更新成功");

			try {
				String suitAccessToken = wxCpTpService.getSuiteAccessToken();
				log.info("suitAccessToken:\\n{}", suitAccessToken);
			} catch (WxErrorException e) {
				log.error("getSuiteAccessToken err:", e);
			}
		  return null;
	  }
}
